package com.wwwarehouse.resource_center.fragment.createobject.createtools;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.google.android.flexbox.FlexboxLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.GetImportToolIndexBean;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.eventbus_event.UploadSuccessEvent;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.ImAbstractObjectBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.bean.goods.RequestBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.CategoryEvent;
import com.wwwarehouse.resource_center.eventbus_event.ChooseUnitEvent;
import com.wwwarehouse.resource_center.eventbus_event.GoodsEvent;
import com.wwwarehouse.resource_center.eventbus_event.ImagePathEvent;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.eventbus_event.StringEvent;
import com.wwwarehouse.resource_center.eventbus_event.ToolTagEvent;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.ChooseUnitFragment;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.CreateGoodsFragment;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.GoodsAttributesProtectFragment;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.GoodsSelectTheLabelParentFragment;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.UploadPicFragment;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;
import com.wwwarehouse.resource_center.utils.UploadPicUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.resource_center.constant.ResourceConstant.PATH_CREATETOOLS;

/**
 * Description: 创建工具
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/7 14:28
 */
@Route(path = PATH_CREATETOOLS)
public class CreateToolsFragment extends TheParentFragment implements View.OnClickListener {

    public RelativeLayout mToolsName;
    public RelativeLayout mToolsPicture;
    public LinearLayout mLLPicture;
    public RelativeLayout mToolsCategory;
    public LinearLayout mToolsDescribe;
    public RelativeLayout mToolsLabel;
    public RelativeLayout mToolsAttribute;
    public TextView mName;
    public TextView mCategory;
    public ImageView mPic;
    public TextView mDes;
    public TextView img_descrip;
    public LinearLayout mLLLaber;
    public FlexboxLayout mLaber;
    public FlexboxLayout mAttr;
    public ImageView mImageAttribute;
    public TextView mTextAttribute;
    public ImageView no_pic;
    public ElasticScrollView elasticScrollView;
    public boolean isShowFinish = true;
    public boolean isShowToImport = true;
    public AutoClickButton mToPCImport;
    public String path;
    public ArrayList<GoodsSelectLabelBean.ListBean> data;
    public ArrayList<GoodsAttributesBean.ListBean> listBeen;
    public String categoryUkid;
    public String ownerUkid;
    public File mFile;
    public FileUploadBean fileUploadBean;
    public String operationUkid;
    public String toolName;
    public String toolDes;
    public ImageView img_name;
    public TextView mUnit;
    public String unitUkid;
    private RelativeLayout mToolsTag;
    public TextView mTag;
    private RelativeLayout mRelUnit;
    public String mTagMsg;
    public String abstractObjectUkid;
    public int pos;  //当前第几页
    protected String mUnitCharacter="";
    @Override
    protected int getContentId() {
        return R.layout.fragment_create_tools_c;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof CreateToolsFragment){
            mActivity.setTitle(R.string.create_tools);
        }
    }

    @Override
    protected void init(View view) {
        mToolsName = (RelativeLayout) view.findViewById(R.id.rel_name);
        mToolsPicture = (RelativeLayout) view.findViewById(R.id.rel_picture);
        mLLPicture = (LinearLayout) view.findViewById(R.id.ll_pic);
        mToolsCategory = (RelativeLayout) view.findViewById(R.id.rel_category);
        mToolsDescribe = (LinearLayout) view.findViewById(R.id.rel_describe);
        mToolsLabel = (RelativeLayout) view.findViewById(R.id.rel_label);
        mToolsAttribute = (RelativeLayout) view.findViewById(R.id.rel_attribute);
        mToolsTag = findView(view, R.id.rel_tag);
        mTag = (TextView) view.findViewById(R.id.tv_tag);
        elasticScrollView = (ElasticScrollView) view.findViewById(R.id.goods_scroll);
        mToPCImport = (AutoClickButton) view.findViewById(R.id.import_to_pc_btn);
        mToPCImport.setTextColor(Color.parseColor("#FFFFFF"));
        mLLLaber = (LinearLayout) view.findViewById(R.id.ll_laber);
        mLaber = (FlexboxLayout) view.findViewById(R.id.fl_laber);
        mAttr = (FlexboxLayout) view.findViewById(R.id.fl_attr);
        mLLLaber.setVisibility(View.VISIBLE);
        img_name = findView(view, R.id.img_name);
        mPic = (ImageView) view.findViewById(R.id.iv_pic);
        no_pic = findView(view, R.id.no_pic);
        mName = (TextView) view.findViewById(R.id.tv_name);
        mCategory = (TextView) view.findViewById(R.id.tv_category);
        mDes = (TextView) view.findViewById(R.id.tv_des);
        img_descrip = findView(view, R.id.img_descrip);
        mRelUnit = (RelativeLayout) view.findViewById(R.id.rel_unit);
        mUnit = (TextView) view.findViewById(R.id.tv_unit);


        EventBus.getDefault().register(this);

        mImageAttribute = (ImageView) view.findViewById(R.id.iv_att);
        mTextAttribute = (TextView) view.findViewById(R.id.tv_att);


        mToolsAttribute.setSelected(false);
        mImageAttribute.setSelected(false);
        mTextAttribute.setSelected(false);
        mToolsPicture.setSelected(false);

        Bundle bundle = getArguments();
        if (bundle != null) {
            CardDeskTaskResponseBean.TaskBean taskBean = (CardDeskTaskResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);
            if (null != taskBean) {
                this.ownerUkid = taskBean.getBelongBusiness();
                this.operationUkid = taskBean.getCardUkid();
            } else {
                CardDeskFunctionResponseBean.TaskBean mBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
                if (null != mBean) {
//                        this.operationUkid = mBean.getTaskTypeUkid();
                    this.ownerUkid = mBean.getBusinessId();
                }
            }
        }

        mToolsName.setOnClickListener(this);
        mToolsPicture.setOnClickListener(this);
        mToolsCategory.setOnClickListener(this);
        mToolsDescribe.setOnClickListener(this);
        mToolsLabel.setOnClickListener(this);
        if (mToolsTag!=null){
            mToolsTag.setOnClickListener(this);
        }
        if (mRelUnit!=null){
            mRelUnit.setOnClickListener(this);
        }
        mToolsAttribute.setOnClickListener(null);


        elasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {
                mToPCImport.setVisibility(View.GONE);
                if (isShowFinish) {
                    ((BaseCardDeskActivity) mActivity).showTitleBt1();
                    ((BaseCardDeskActivity) mActivity).setTitleBt1Text(R.string.complete);
                    boolean verification = verification();
                    if (verification) {
                        ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);
                    } else {
                        ((BaseCardDeskActivity) mActivity).setTitleBt1Enable(false);
                    }

                } else {
                    ((BaseCardDeskActivity) mActivity).hideTitleBt();
                }
                isShowFinish = !isShowFinish;
            }

            @Override
            public void onUpPull() {
                ((BaseCardDeskActivity) mActivity).hideTitleBt();
                if (isShowToImport) {
                    mToPCImport.setVisibility(View.VISIBLE);
                    mToPCImport.setEnabled(true);
                    mToPCImport.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("businessUnitId", ownerUkid);
                            map.put("templateType", "工具");
                            if (operationUkid != null && !operationUkid.isEmpty()) {
                                map.put("previousOperationUkid", operationUkid); //任务UKID
                            }

                            httpPost(Constant.GET_IMPORT_TOOLS_INDEX, map, 1, true, "");
                        }
                    });
                } else {
                    mToPCImport.setVisibility(View.GONE);
                }
                isShowToImport = !isShowToImport;
            }
        });

    }

    @Override
    public void titleBt1Click() {
        if (peekFragment() instanceof CreateToolsFragment) {
            if (path != null && !path.isEmpty()) {
                showProgressDialog("图片上传中...");
                UploadPicUtil.getInstance().uploadPic(path, mFile, getActivity());
            } else {
                request(ResourceConstant.SAVE_OBJECT, "");
            }
        }
    }

    //保存信息
    public void request(String url, String cardType) {
        try {
            Map<String, Object> map = new HashMap<>();
            //标签列表
            List<RequestBean> labelList = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                RequestBean labelBean = new RequestBean();
                labelBean.setLabelUkid(data.get(i).getLabelUkid() + "");
                labelList.add(labelBean);
            }
            //条码列表    type:条码为1；69码为4
            List<RequestBean> identifyList = new ArrayList<>();
            RequestBean barCodeBean = new RequestBean();
            barCodeBean.setIdentifyCode(mTag.getText().toString().trim());
            barCodeBean.setIdentifyType("0");
            identifyList.add(barCodeBean);

            //分类列表
            List<RequestBean> categoryList = new ArrayList<>();
            RequestBean categoryCodeBean = new RequestBean();
            categoryCodeBean.setCategoryUkid(categoryUkid);
            categoryList.add(categoryCodeBean);

            //属性列表
            List<RequestBean> attributeList = new ArrayList<>();

            for (int i = 0; i < listBeen.size(); i++) {
                RequestBean attributeBean = new RequestBean();
                attributeBean.setAttributeUkid(listBeen.get(i).getAttributeUkid() + "");
                attributeBean.setAttributeValue(listBeen.get(i).getAttributeValue());
                attributeList.add(attributeBean);
            }
            ImAbstractObjectBean imAbstractObjectBean = new ImAbstractObjectBean();

            //媒体列表
            if (fileUploadBean != null) {
                List<RequestBean> mediaList = new ArrayList<>();
                RequestBean mediaBean = new RequestBean();
                mediaBean.setMediaName(fileUploadBean.getData().get(0).getName());
                mediaBean.setMediaType("0");
                mediaBean.setRelatedUrl(fileUploadBean.getData().get(0).getPath());
                mediaList.add(mediaBean);
                imAbstractObjectBean.setMediaList(mediaList); //媒体列表

            }
            if (!TextUtils.isEmpty(abstractObjectUkid)) {
                imAbstractObjectBean.setAbstractObjectUkid(abstractObjectUkid);
            }
            imAbstractObjectBean.setType("CREATE_H_TOOLS"); //1-商品、2-工具、3-店铺、4-仓库
            imAbstractObjectBean.setName(mName.getText().toString().trim());   //物的名称
            imAbstractObjectBean.setLabelList(labelList);  //标签列表
            imAbstractObjectBean.setIntroduction(mDes.getText().toString().trim());  //描述
            imAbstractObjectBean.setCategoryList(categoryList);//分类列表
            imAbstractObjectBean.setIdentifyList(identifyList); //标示码列表
            imAbstractObjectBean.setOwnerUkid(ownerUkid);  //业务单位UKid
            imAbstractObjectBean.setAttributeList(attributeList); //属性列表
            if (unitUkid != null && !unitUkid.isEmpty()) {
                imAbstractObjectBean.setUnitUkid(unitUkid); //单位
            }

            if (!TextUtils.isEmpty(cardType)) {
                map.put("cardType", cardType);
            }
            map.put("imAbstractObject", imAbstractObjectBean);
            if (operationUkid != null && !operationUkid.isEmpty()) {
                map.put("operationUkid", operationUkid); //任务UKID
            }

            httpPost(url, map, 0, true, "");
        } catch (Exception e) {
            LogUtils.showLog("商品信息保存失败!");
        }


    }

    boolean verification() {
        boolean b = false;
        try {
            //验证保存信息
            String name = mName.getText().toString(); //名称
            String category = mCategory.getText().toString();//分类
            String des = mDes.getText().toString(); //描述
            String barCode = mTag.getText().toString();//条码标示信息
            if (!name.isEmpty() && !category.isEmpty() && !des.isEmpty() && !barCode.isEmpty() && data != null && !data.isEmpty() && listBeen != null && !listBeen.isEmpty())
                b = true;
            if (getString(R.string.tool_name).equals(name)) {
                b = false;
            }
            if (getString(R.string.goods_describe).equals(des)){
                b = false;
            }
        } catch (Exception e) {
            b = false;
        }
        return b;
    }


    @Override
    public void requestDatas() {

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        try {

            if (what == 0) {
                if (TextUtils.equals("0", commonClass.getCode())) {
                    EventBus.getDefault().post(new GoodsEvent(GoodsEvent.GOODS_MODIFY,pos));
                    EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                } else {
                    toast(commonClass.getMsg());
                }
            } else if (what == 1) {
                if (commonClass != null && commonClass.getData() != null) {
                    GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
                    if (bean != null) {
                        popFragment();
                        if (bean.getPageType() == 1) {
                            //跳转到生成短链接的页面
                            Bundle bundle = new Bundle();
                            bundle.putString(Constant.KEY_INTENT_IMPORT_TYPE, "IMPORT_TOOL");
                            bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TOOL, bean);
                            pushFragment("/contract/ImportHaveSortFragment", bundle);

                        } else if (bean.getPageType() == 2) {
                            //跳转到时间页面
                            Bundle bundle = new Bundle();
                            bundle.putString(Constant.KEY_INTENT_IMPORT_TYPE, "IMPORT_TOOL");
                            bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TIME, bean);
                            pushFragment("/contract/DurationTimeFragment", bundle);

                        }
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onReLoad(int code) {

    }

    public void onEventMainThread(Object event) {

        if (event instanceof CategoryEvent) {
            mAttr.removeAllViews();
            mTextAttribute.setVisibility(View.VISIBLE);
            mCategory.setText(((CategoryEvent) event).getMsg().getName());
            categoryUkid = ((CategoryEvent) event).getMsg().getCategoryUkid();
            mToolsAttribute.setSelected(true);
            mImageAttribute.setSelected(true);
            mTextAttribute.setSelected(true);
            mToolsAttribute.setOnClickListener(this);
            listBeen=null;
            popFragment();
        } else if (event instanceof StringEvent && "create_tools_name".equals(((StringEvent) event).getFrom())) {
            toolName = ((StringEvent) event).getMsg();
            mName.setText(toolName);
            popFragment();
        } else if (event instanceof ImagePathEvent) {
            //显示图片的配置
            path = ((ImagePathEvent) event).getMsg();
            mFile = ((ImagePathEvent) event).getmFile();
            mLLPicture.setVisibility(View.GONE);
            mPic.setVisibility(View.VISIBLE);
            mToolsPicture.setSelected(true);
            ImageLoader.getInstance().displayImage("file://" + ((ImagePathEvent) event).getMsg(), mPic);

        } else if (event instanceof StringEvent && "descrip".equals(((StringEvent) event).getFrom())) {
            toolDes = ((StringEvent) event).getMsg();
            mDes.setText(toolDes);
            popFragment();
        } else if (event instanceof GoodsEvent && ((GoodsEvent) event).getCode() == GoodsEvent.GOODS_LABEL_SELECT_NOTICE) {
            //标签
            try {

                data = new ArrayList<>();
                ArrayList<GoodsSelectLabelBean.ListBean> dataList = (ArrayList<GoodsSelectLabelBean.ListBean>) ((GoodsEvent) event).getData();
                data.addAll(dataList);
                setLaber(data);


            } catch (Exception e) {

            }


        } else if (event instanceof RulesEvent && GoodsEvent.GOODS_ATTRIBUTE_SELECT_NOTICE == ((RulesEvent) event).getCode()) {
            try {
                listBeen = (ArrayList<GoodsAttributesBean.ListBean>) ((RulesEvent) event).getData();
                setAttributes(listBeen);


            } catch (Exception e) {

            }


        } else if (event instanceof UploadSuccessEvent) {
            dismissProgressDialog();
            fileUploadBean = ((UploadSuccessEvent) event).getFileUploadBean();
            if (!TextUtils.isEmpty(abstractObjectUkid)) {
                request(ResourceConstant.UPDATE_ABSTRACT_OBJECT,"UPDATE_H_TOOLS");
            }else {
                request(ResourceConstant.SAVE_OBJECT, "");
            }
        } else if (event instanceof BackListenerEvent) {

            if ("CreateToolsFragment".equals(((BackListenerEvent) event).getMsg())) {
                DialogTools.getInstance().showTCDialog(getContext(), "提示", "返回将不会保存当前编辑内容，确定返回吗？", "再看看", "返回", null, new DialogTools.DismissListener() {
                    @Override
                    public void setDismissListener(Dialog dialog, View view) {
                        dialog.dismiss();
                        popFragment();
                    }
                });
            }
        } else if (event instanceof ChooseUnitEvent) {
            if (!TextUtils.isEmpty(((ChooseUnitEvent) event).getMsg().getUnitCharacter())){
                mUnitCharacter ="（"+ ((ChooseUnitEvent) event).getMsg().getUnitCharacter()+"）";
            }
            if (!TextUtils.isEmpty(((ChooseUnitEvent) event).getMsg().getUnitCharacter())) {
                mUnit.setText(((ChooseUnitEvent) event).getMsg().getUnitName() + "（" + ((ChooseUnitEvent) event).getMsg().getUnitCharacter() + "）");
            } else {
                mUnit.setText(((ChooseUnitEvent) event).getMsg().getUnitName());
            }
            unitUkid = ((ChooseUnitEvent) event).getMsg().getUnitUkid();
            popFragment();
        } else if (event instanceof ToolTagEvent) {
            mTagMsg = ((ToolTagEvent) event).getMsg();
            mTag.setText(mTagMsg);
            popFragment();
        }
    }

    public void setAttributes(ArrayList<GoodsAttributesBean.ListBean> listBeen) {
        mAttr.removeAllViews();
        mTextAttribute.setVisibility(View.GONE);
        if (listBeen.size() >= 4) {
            for (int i = 0; i < 4; i++) {
                TextView textView = new TextView(getContext());
//                        textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
                textView.setWidth(ConvertUtils.dip2px(getContext(), 130));
                textView.setTextColor(Color.parseColor("#FFFFFF"));
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 10), ConvertUtils.dip2px(getContext(), 10), 0, 0);
                textView.setLayoutParams(layoutParams);
                textView.setText(listBeen.get(i).getAttributeName() + ": " + listBeen.get(i).getAttributeValue());
                mAttr.addView(textView);
            }
        } else {
            for (int i = 0; i < listBeen.size(); i++) {
                TextView textView = new TextView(getContext());
                textView.setWidth(ConvertUtils.dip2px(getContext(), 130));
//                        textView.setHeight(ConvertUtils.dip2px(getContext(), 100));
                textView.setTextColor(Color.parseColor("#FFFFFF"));
                textView.setTextSize(12);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 10), ConvertUtils.dip2px(getContext(), 8), 0, 0);
                textView.setLayoutParams(layoutParams);
                textView.setText(listBeen.get(i).getAttributeName() + ": " + listBeen.get(i).getAttributeValue());
                mAttr.addView(textView);

            }
        }
    }

    public void setLaber(ArrayList<GoodsSelectLabelBean.ListBean> data) {
        mLLLaber.setVisibility(View.GONE);
        mLaber.removeAllViews();
        if (data.size() < 3) {
            for (int i = 0; i < data.size(); i++) {
                TextView textView = getTextView();

                textView.setTextColor(Color.parseColor("#337CFF"));
                textView.setText(data.get(i).getLabelName());
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setSelected(true);

                mLaber.addView(textView);
            }
            for (int i = 0; i < 3 - data.size(); i++) {
                TextView textView = getTextView();
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setSelected(false);
                mLaber.addView(textView);
            }
        } else if (data.size() >= 3) {
            for (int i = 0; i <= 2; i++) {
                TextView textView = new TextView(getContext());
                textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
                textView.setHeight(ConvertUtils.dip2px(getContext(), 27));
                textView.setBackgroundResource(R.drawable.laber_bg_selector);
                textView.setTextColor(Color.parseColor("#337CFF"));
                textView.setTextSize(11);
                textView.setGravity(Gravity.CENTER);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                if (i == 2) {
                    textView.setText(R.string.ellipsis);
                } else {
                    textView.setText(data.get(i).getLabelName());
                }
                textView.setSelected(true);
                FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 6), ConvertUtils.dip2px(getContext(), 8), 0, 0);
                textView.setLayoutParams(layoutParams);
                mLaber.addView(textView);
            }

        }
    }

    @NonNull
    public  TextView getTextView() {
        TextView textView = new TextView(getContext());
        textView.setWidth(ConvertUtils.dip2px(getContext(), 100));
        textView.setHeight(ConvertUtils.dip2px(getContext(), 27));
        textView.setBackgroundResource(R.drawable.laber_bg_selector);
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(12);
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(ConvertUtils.dip2px(getContext(), 6), ConvertUtils.dip2px(getContext(), 8), 0, 0);
        textView.setLayoutParams(layoutParams);
        return textView;
    }


    @Override
    public void onClick(View v) {
        Bundle b = new Bundle();
        b.putString("ownerUkid", ownerUkid);
        int i = v.getId();
        initState();
        if (i == R.id.rel_name) {
            //工具名称
            ToolsAddNameFragment toolsAddNameFragment = new ToolsAddNameFragment();
            b.putString("name", toolName);
            toolsAddNameFragment.setArguments(b);
            pushFragment(toolsAddNameFragment);
        } else if (i == R.id.rel_picture) {
            //图片
            UploadPicFragment uploadPicFragment = new UploadPicFragment();
            Bundle bundle = new Bundle();
            if (null != fileUploadBean) {
                try {
                    bundle.putString("path", fileUploadBean.getData().get(0).getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                bundle.putString("path", path);
            }
            uploadPicFragment.setArguments(bundle);
            pushFragment(uploadPicFragment);

        } else if (i == R.id.rel_category) {
            //分类
            ToolCategoryFragment toolCategoryFragment = new ToolCategoryFragment();
            toolCategoryFragment.setArguments(b);

            pushFragment(toolCategoryFragment);
        } else if (i == R.id.rel_describe) {
            //描述
            ToolsDescripFragment toolsDescripFragment = new ToolsDescripFragment();

            b.putString("des", toolDes);
            toolsDescripFragment.setArguments(b);

            pushFragment(toolsDescripFragment);

        } else if (i == R.id.rel_label) {
            //标签
            sp.putValue("businessId", ownerUkid);
            GoodsSelectTheLabelParentFragment fragment = new GoodsSelectTheLabelParentFragment();
            b.putParcelableArrayList("LabelList", data);
            fragment.setArguments(b);
            pushFragment(fragment);
        } else if (i == R.id.rel_attribute) {
            //属性
            GoodsAttributesProtectFragment goodsAttributesSelectParentFragment = new GoodsAttributesProtectFragment();
            Bundle bundle = new Bundle();
            bundle.putString("categoryUkid", "");
            bundle.putString("businessId", ownerUkid);
            bundle.putParcelableArrayList("AttributeList", listBeen);
            goodsAttributesSelectParentFragment.setArguments(bundle);
            pushFragment(goodsAttributesSelectParentFragment);
        } else if (i == R.id.rel_unit) {
            //单位
            ChooseUnitFragment chooseUnitFragment = new ChooseUnitFragment();
            Bundle bundle = new Bundle();
            bundle.putString("ownerUkid", ownerUkid);
            bundle.putString("unitUkid", unitUkid);
            chooseUnitFragment.setArguments(bundle);
            pushFragment(chooseUnitFragment);
        } else if (i == R.id.rel_tag) {
            //标示
            ToolsTagFragment toolsTagFragment = new ToolsTagFragment();
            b.putString("barcode", mTagMsg);
            toolsTagFragment.setArguments(b);
            pushFragment(toolsTagFragment);
        }
    }

    void initState() {
        ((BaseCardDeskActivity) mActivity).hideTitleBt();
        mToPCImport.setVisibility(View.GONE);
        isShowFinish = true;
        isShowToImport = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        initState();
        EventBus.getDefault().unregister(this);
    }

}
